package com.h2psoft.truyen.tranh.server;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.h2psoft.truyen.tranh.constants.Constants;
import com.h2psoft.truyen.tranh.objects.ObjChaper;
import com.h2psoft.truyen.tranh.objects.ObjComic;

import vn.persoft.extra.http.LbHttp;
import vn.persoft.extra.secure.LbEcrypt;
import android.util.Log;

public class ServerHandling {
	public static final String tag = ServerHandling.class.getName();

	// ***** ENCRYPT METHOD *****

	public String encrypt(String param) {
		return new LbEcrypt().encrypt(param);
	}

	// ***** WORK WITH COMIC *****
	public boolean getListComic(ArrayList<ObjComic> comicArr) {

		String url = Constants.WEB_SERVICE_URL
				+ encrypt(Constants.PARA_METHOD + Constants.METHOD_GET_LIST_COMIC + "&" + Constants.PARA_APP_ID + "=" + Constants.APP_ID);
		Log.e(tag, url);
		String json = LbHttp.httpGET_UTF8(url);
		convertJsonToObjComic(comicArr, json);
		return (json.length() > 0 ? false : true);
	}

	public void convertJsonToObjComic(ArrayList<ObjComic> comicArr, String json) {
		if (comicArr == null)
			return;
		try {
			if (!json.equalsIgnoreCase("null")) {
				JSONArray jArray = new JSONArray(json);
				for (int i = 0; i < jArray.length(); i++) {
					JSONObject json_data = jArray.getJSONObject(i);
					ObjComic objComic = new ObjComic();
					if (json_data.has(Constants.PARA_ID))
						objComic.setId(json_data.getInt(Constants.PARA_ID));
					if (json_data.has(Constants.PARA_COUNT))
						objComic.setCount(json_data.getInt(Constants.PARA_COUNT));
					if (json_data.has(Constants.PARA_NAME))
						objComic.setName(json_data.getString(Constants.PARA_NAME));
					if (json_data.has(Constants.PARA_STATUS))
						objComic.setStatus(json_data.getString(Constants.PARA_STATUS));
					if (json_data.has(Constants.PARA_AUTHOR))
						objComic.setAuthor(json_data.getString(Constants.PARA_AUTHOR));
					if (json_data.has(Constants.PARA_ICON))
						objComic.setIconUrl(json_data.getString(Constants.PARA_ICON));
					comicArr.add(objComic);
				}
			}
		} catch (JSONException e) {
			Log.e(tag, e.toString());
		}
	}

	// ***** WORK WITH CHAPER *****
	public boolean getListChaper(ArrayList<ObjChaper> chapArr, int catId, int start) {

		String url = Constants.WEB_SERVICE_URL
				+ encrypt(Constants.PARA_METHOD + Constants.METHOD_GET_LIST_CHAP + "&" + Constants.PARA_APP_ID + "=" + Constants.APP_ID + "&"
						+ Constants.PARA_CAT_ID + "=" + catId + "&" + Constants.PARA_START + "=" + start);

		Log.e(tag, url);
		String json = LbHttp.httpGET_UTF8(url);
		convertJsonToObjChaper(chapArr, json);
		return (json.length() > 0 ? false : true);
	}

	public boolean getNewChaper(ArrayList<ObjChaper> chapArr, int start) {

		String url = Constants.WEB_SERVICE_URL
				+ encrypt(Constants.PARA_METHOD + Constants.METHOD_GET_NEW_CHAP + "&" + Constants.PARA_START + "=" + start);

		// Log.e(tag, url);
		String json = LbHttp.httpGET_UTF8(url);
		convertJsonToObjChaper(chapArr, json);
		return (json.length() > 0 ? false : true);
	}

	public void convertJsonToObjChaper(ArrayList<ObjChaper> chapArr, String json) {
		if (chapArr == null)
			return;
		try {
			if (!json.equalsIgnoreCase("null")) {
				JSONArray jArray = new JSONArray(json);
				for (int i = 0; i < jArray.length(); i++) {
					JSONObject json_data = jArray.getJSONObject(i);
					ObjChaper objChaper = new ObjChaper();
					if (json_data.has(Constants.PARA_ID))
						objChaper.setId(json_data.getInt(Constants.PARA_ID));
					if (json_data.has(Constants.PARA_CONTENT))
						objChaper.setContent(json_data.getString(Constants.PARA_CONTENT).trim());
					if (json_data.has(Constants.PARA_IMAGE_URL))
						objChaper.setImgUrl(json_data.getString(Constants.PARA_IMAGE_URL).trim());
					if (json_data.has(Constants.PARA_IMAGE_DOWNLOAD))
						objChaper.setSecondImgUrl(json_data.getString(Constants.PARA_IMAGE_DOWNLOAD).trim());
					chapArr.add(objChaper);
				}
			}
		} catch (JSONException e) {
			Log.e(tag, e.toString());
		}
	}
}
